/**
 * Created by JetBrains WebStorm.
 * User: liucw
 * Date: 12-3-18
 * Time: 下午10:17
 * To change this template use File | Settings | File Templates.
 */
$(document).ready(function()
{
    var canvas = $("#myCanvas");
    context = canvas.get(0).getContext("2d");

    canvasWidth = canvas.width();
    canvasHeight = canvas.height();

    //使窗口全屏
    function resizeCanvas()
    {
        canvas.attr("width",$(window).get(0).innerWidth);
        canvas.attr("height",$(window).get(0).innerHeight);
        canvasWidth = canvas.width();
        canvasHeight = canvas.height();
    }
    $(window).resize(resizeCanvas);
    resizeCanvas();
    //使窗口全屏！
    var playAnimation = true;

    var startButton = $("#startAnimation");
    var stopButton = $("#stopAnimation");

    stopButton.hide();
    startButton.click(function()
    {
        $(this).hide();
        stopButton.show();
        playAnimation = true;
        animate();
    });
    stopButton.click(function()
    {
        $(this).hide();
        startButton.show();
        playAnimation = false;
    });

    var Asteroid = function(x,y,radius,mass,vX,vY,aX,aY)
    {
        this.x = x;
        this.y = y;
        this.radius = radius;
        this.mass = mass;
        //速度
        this.vX = vX;
        this.vY = vY;
        //加速度
        this.aX = aX;
        this.aY = aY;
    };

    var asteroids = new Array();
    for(var  i = 0;i < 10 ; i++  )
    {
        var x = 20 + ((canvasWidth-40)*Math.random());
        var y = 20 + ((canvasHeight-40)*Math.random());
        var radius = 10+50*Math.random();
        var mass = radius/2;
        var vX = Math.random()*8 - 4;
        var vY = Math.random()*8 - 4;
        var aX = Math.random()*0.4 - 0.2;
        var aY = Math.random()*0.4 - 0.2;
        asteroids.push(new Asteroid(x,y,radius,mass,vX,vY,aX,aY));
    };

    function animate()
    {

        if(playAnimation)
        {
            context.clearRect(0,0,canvasWidth,canvasHeight);
            context.fillStyle = "rgb(255,255,255)";

            var arraylength = asteroids.length;
            for(var i = 0 ;i < arraylength;i++)
            {
                var tmpAsteroid = asteroids[i];
                //判断相互之间是否碰撞
                for(var j = i + 1; j < arraylength; j++)
                {
                    var tmpAsteroidB = asteroids[j];
                    var dX = tmpAsteroidB.x - tmpAsteroid.x;
                    var dY = tmpAsteroidB.y - tmpAsteroid.y;
                    var distance = Math.sqrt((dX*dX) + (dY*dY));

                    if(distance < (tmpAsteroid.radius + tmpAsteroidB.radius))
                    {
                        var angle = Math.atan2(dY,dX);
                        var sine = Math.sin(angle);
                        var cosine = Math.cos(angle);

                        var x = 0;
                        var y = 0;
                        var xB = dX * cosine + dY * sine;
                        var yB = dY * cosine - dX * sine;
                        var vX = tmpAsteroid.vX * cosine + tmpAsteroid.vY * sine;
                        var vY = tmpAsteroid.vY * cosine - tmpAsteroid.vX * sine;
                        var vXb = tmpAsteroidB.vX * cosine + tmpAsteroidB.vY * sine;
                        var vYb = tmpAsteroidB.vY * cosine - tmpAsteroidB.vX * sine;
                        //vX *= -1;
                        //vXb *= -1;
                        //动量定理
                        var vTotal = vX - vXb;
                        vX = ((tmpAsteroid.mass - tmpAsteroidB.mass) * vX + 2 * tmpAsteroidB.mass * vXb)/(tmpAsteroid.mass + tmpAsteroidB.mass);
                        vXb = vTotal + vX;


                        xB = x + (tmpAsteroid.radius + tmpAsteroidB.radius);
                        tmpAsteroid.x = tmpAsteroid.x + (x * cosine - y * sine);
                        tmpAsteroid.y = tmpAsteroid.y + (y * cosine + x * sine);
                        tmpAsteroidB.x = tmpAsteroid.x + (xB * cosine - yB * sine);
                        tmpAsteroidB.y = tmpAsteroid.y + (yB * cosine + xB * sine);
                        tmpAsteroid.vX = vX * cosine - vY * sine;
                        tmpAsteroid.vY = vY * cosine + vX * sine;
                        tmpAsteroidB.vX = vXb * cosine - vYb * sine;
                        tmpAsteroidB.vY = vYb * cosine + vXb * sine;

                    }
                }
                //判断是否碰壁
                if(tmpAsteroid.x - tmpAsteroid.radius <= 0)
                {
                    tmpAsteroid.x = tmpAsteroid.radius;
                    tmpAsteroid.vX *= -1;
                    tmpAsteroid.aX *= -1;
                }
                else if(tmpAsteroid.x + tmpAsteroid.radius >= canvasWidth)
                {
                    tmpAsteroid.x = canvasWidth - tmpAsteroid.radius;
                    tmpAsteroid.vX *= -1;
                    tmpAsteroid.aX *= -1;
                }
                if(tmpAsteroid.y - tmpAsteroid.radius <= 0)
                {
                    tmpAsteroid.y = tmpAsteroid.radius;
                    tmpAsteroid.vY *= -1;
                    tmpAsteroid.aY *= -1;
                }
                else if(tmpAsteroid.y + tmpAsteroid.radius >= canvasHeight)
                {
                    tmpAsteroid.y = canvasHeight - tmpAsteroid.radius;
                    tmpAsteroid.vY *= -1;
                    tmpAsteroid.aY *= -1;
                }
//                //摩擦力
//                if(Math.abs(tmpAsteroid.vX) > 0.1)
//                    tmpAsteroid.vX *= 0.9;
//                else
//                    tmpAsteroid.vX = 0;
//                if(Math.abs(tmpAsteroid.vY) > 0.1)
//                    tmpAsteroid.vY *= 0.9;
//                else
//                    tmpAsteroid.vY = 0;

                //加速度
                tmpAsteroid.vX += tmpAsteroid.aX;
                tmpAsteroid.vY += tmpAsteroid.aY;

                //速度
                tmpAsteroid.x += tmpAsteroid.vX;
                tmpAsteroid.y += tmpAsteroid.vY;

                //绘制
                context.beginPath();
                context.arc(tmpAsteroid.x,tmpAsteroid.y,tmpAsteroid.radius,0,2*Math.PI,false);
                context.closePath();
                context.fill();
            }
            setTimeout(animate,33);
        }

    }
    //animate();
}
)

